package com.nd.qa.shareplate.dom4j;

import java.io.FileOutputStream;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class DOM4JCreateXMLDemo {
	
	public static void main(String[] args) {
		//SAX是基于事件流的解析,DOM是基于XML文档树结构的解析
		try {
			Document document = DocumentHelper.createDocument();
			//创建根节点
			Element root = document.addElement("cars");
			Element supercarElement = root.addElement("supercars").addAttribute("company", "Ferrai");
			supercarElement.addElement("carname").addAttribute("type", "basketball").addText("Ferrari 101");
			supercarElement.addElement("carname").addAttribute("type", "sports").addText("Ferrari 202");
			OutputFormat format = OutputFormat.createPrettyPrint();
			XMLWriter writer;
			//writer = new XMLWriter(System.out, format);
			writer = new XMLWriter(new FileOutputStream("E:\\baiduyun\\test.xml"), format);
			writer.write(document);
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
